WWL.rte = function ( name )
{
	var rte = new WWL ( 'rte', name );

	rte.width = 400;
	rte.height = 300;

	rte._edit_mode = false;
	rte._curr_html = "";

	rte.to_string = function ()
	{
		var name = "cnt:swf:" + rte.id;
		var w = rte.width + 40, h = rte.height;
		var r = { w: w, h: h, id: rte.id, name: name, rte_name: rte.name, txt_w: w - 64, txt_cnt_w: w - 40 };
		var t = '<div class="rte_cnt" style="width: %(w)spx; height: %(h)spx;" id="%(name)s">' + 
			'<div style="overflow-y: auto; width: %(txt_cnt_w)spx; height: %(h)spx;">' +
			'	<div id="txt:swf:%(id)s" style="width: %(txt_w)spx;"></div>' +
			'</div>' + 
			'<div id="btn1:swf:%(id)s" class="btn1" onclick="WWL.rte.edit(\'%(rte_name)s\',\'swf\')" title="Modifica il testo"></div>' +
			'<div id="btn2:swf:%(id)s" class="btn2" onclick="WWL.rte.edit(\'%(rte_name)s\')" title="Conferma modifiche"></div>' +
			'<div id="btn3:swf:%(id)s" class="btn3" onclick="WWL.rte.edit(\'%(rte_name)s\',\'undo\')" title="Annulla le modifiche"></div>' +
			'<div id="btn4:swf:%(id)s" class="btn4" onclick="WWL.rte.edit(\'%(rte_name)s\',\'html\')" title="Modifica HTML"></div>' +

			'</div>';

		var s = String.formatDict ( t, r );

		return s;

	};

	rte.to_swf = function ()
	{
		var name = "swf:" + rte.id;
		var w = rte.width, h = rte.height;

		var s = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="' + name + '" width="' + w + '" height="' + h + '" ' +
                        'codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"> ' + 
                        '<param name="movie" value="' + WWL._libbase + '/rte/gfx/RTE.swf" /> ' + 
                        '<param name="quality" value="high" /> ' + 
                        '<param name="bgcolor" value="#869ca7" /> ' + 
                        '<param name="allowNetworking" value="all" /> ' + 
                        '<param name="allowScriptAccess" value="always" /> ' +
                        '<embed src="' + WWL._libbase + '/rte/gfx/RTE.swf" quality="high" bgcolor="#869ca7" width="' + w + '" height="' + h + '" name="' + name + '" align="middle" ' +
                        'play="true" loop="false" quality="high" allowNetworking="all" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer"> ' +
                        '</embed> </object>';

		return s;
	};

	rte.to_textarea = function ()
	{
		var id = "txtarea:" + rte.id;
		var s = '<textarea id="' + id + '" class="rte_textarea" style="width: 100%; height: 100%">' + rte._curr_html + '</textarea>';
		return s;
	};

	rte._get_obj = function ()
	{
		var name = "swf:" + rte.id;
		if ( navigator.appName.indexOf ( "Microsoft" ) != -1) 
			return window [ name ];

        	return document [ name ];
	};

	rte.get_html = function ()
	{
		var res = "";

		switch ( rte._edit_mode )
		{
			case "swf":
				var obj = rte._get_obj ();
				res = rte._filter_out ( obj.getHTML () );
				break;

			case "html":	
				var el = $ ( "txtarea:" + rte.id );
				res = el.value;
				break;

			default:
				res = rte._curr_html;
				break;
		}

		return res;
	};

	rte.set_html = function ( html )
	{
		switch ( rte._edit_mode )
		{
			case "swf":
				rte.set_swf_html ( html );
				break;

			case "html":
				var el = $ ( "txtarea:" + rte.id );
				el.value = html;
				break;

			default:
				$( "txt:swf:" + rte.id, html );
				break;
		}

		rte._curr_html = html;
	};

	rte.set_swf_html = function ( html )
	{
		var obj = rte._get_obj ();

		if ( ! obj.setHTML )
		{
			var _my_obj = rte;
			setTimeout ( function () { _my_obj.set_swf_html ( html ); }, 300 );
		} else
			obj.setHTML ( html );
	};

	rte._filter_out = function ( txt )
	{
		
		// remove some tags
		txt = txt.replace ( /<.?(font|textformat)[^>]*>/gi, "" );

		// make all tags lowercase
		txt = txt.replace ( /<([^>]*)>/g, function ( v ) 
			{
				return v.toLowerCase ();
			} );

		return liwe.utils.map_entities ( txt.htmlEntities () );
	};

	return rte;
};

WWL.rte.edit = function ( rte_name, mode )
{
	var rte = WWL.get_instance ( 'rte', rte_name );
	var cnt = "txt:swf:" + rte.id;

	if ( rte._edit_mode )
	{ 
		if ( mode != "undo" ) rte._curr_html = rte.get_html ();
		rte._edit_mode = false;

		$( "btn1:swf:" + rte.id ).style.display = "block";
		$( "btn4:swf:" + rte.id ).style.display = "block";
		$( "btn2:swf:" + rte.id ).style.display = "none";
		$( "btn3:swf:" + rte.id ).style.display = "none";

		rte.set_html ( rte._curr_html );
	} else {

		rte._edit_mode = mode;

		if ( mode == "html" )
		{
			$( cnt, rte.to_textarea () );
			rte.set_html ( rte._curr_html );
		}
		else
		{
			$( cnt, rte.to_swf () );
			rte.set_swf_html ( rte._curr_html );
		}

		$( "btn1:swf:" + rte.id ).style.display = "none";
		$( "btn4:swf:" + rte.id ).style.display = "none";
		$( "btn2:swf:" + rte.id ).style.display = "block";
		$( "btn3:swf:" + rte.id ).style.display = "block";
	}
};
